package serve;

import po.Good;
import po.GoodDB;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

@WebServlet("/goods")
public class ListGood extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");// 设置生成的文档类型
        PrintWriter out = response.getWriter();// 得到输出字符输出流
        Collection<Good> goods = GoodDB.getAll();
        out.println("<h1>今日热销商品如下：</h1>");
        out.println("<ul>");
        for (Good good : goods) {
            String URL = request.getContextPath()+"/Pur?id="+good.getId();
            out.println("<li>");
            out.println(good.getName() + "&nbsp;&nbsp;&nbsp;<a href='"+URL+"'>"+"点击购买"+"</a><br>");
            out.println("</li>");
        }
        out.println("</ul>");
    }
}